/*
 * StudentReviewPanel.java
 *
 * Created on May 24, 2011, 11:33:46 AM
 */

package org.jdesktop.wonderland.modules.isocial.generic.client;

import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/**
 *
 * @author ryan
 */
public class StudentReviewPanel extends javax.swing.JPanel {

    /** Creates new form StudentReviewPanel */
    public StudentReviewPanel() {
        initComponents();

    }
    public StudentReviewPanel(Map<String, List<String>> data) {
        initComponents();

        DefaultTableModel model = new DefaultTableModel();
        for(String question: data.keySet()) {
            model.addColumn(question, data.get(question).toArray());
        }

        jTable1.setCellSelectionEnabled(false);
        jTable1.setModel(model);
        jTable1.setDefaultRenderer(String.class, new MyCustomTableRenderer());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    class CustomTableModel extends AbstractTableModel {

        private final List<String> questions;
        private final List<String> answers;
        private final Map<String, String> questionsToAnswers;

        public CustomTableModel(List<String> questions, List<String> answers) {
            super();
            this.questions = new ArrayList();
            this.answers = new ArrayList();
            this.questionsToAnswers = new HashMap();
            
            this.questions.addAll(questions);
            this.answers.addAll(answers);
        }
        public int getRowCount() {
            return answers.size();
        }

        public int getColumnCount() {
            return questions.size();
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            if(columnIndex < questions.size()) {
                return questionsToAnswers.get(questions.get(columnIndex));
            }
            return null;
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            if(col < questions.size()) {
                questionsToAnswers.put(questions.get(col), (String)value);
            }
        }

        public void addColumn(String question, String answer) {
            questions.add(question);
            answers.add(answer);
            questionsToAnswers.put(question, answer);
        }

        public void changeAnswer(String question, String answer) {
            questionsToAnswers.remove(question);
            questionsToAnswers.put(question, answer);
        }
    }

    /**
     *
     * @author Kaustubh
     */
    class MyCustomTableRenderer extends JTextArea implements TableCellRenderer {

        public MyCustomTableRenderer() {
            setLineWrap(true);
            setWrapStyleWord(true);
            setOpaque(true);
            setAlignmentX(LEFT_ALIGNMENT);
            setAlignmentY(CENTER_ALIGNMENT);
            setPreferredSize(new Dimension(200, 40));
            this.setEditable(false);
        }

        public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean isFocused, int row, int column) {
            setText((value == null) ? "" : value.toString());
//        int stringWidth = getFontMetrics(getFont()).stringWidth(getText());
//        int headerwidth = table.getColumnModel().getColumn(column).getWidth();
//        if (stringWidth >= headerwidth) {
//            setSize(stringWidth, (int) getPreferredSize().getHeight());
//            table.getColumnModel().getColumn(column).setWidth(stringWidth);
//        } else {
//            setSize(headerwidth, (int) getPreferredSize().getHeight());
//        }
            return this;
        }
    }
}
